package com.trillion.toyou;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;



public class MyViewFragment extends Fragment implements OnClickListener{
	// 뷰 지정
	View view = null;
	

	// db 관련
	DBHelper db = null;
	Data data = null;
	
	// 프래그먼트 관련
	Fragment frag = null;
	FragmentTransaction trans = null;
	Bundle bundle = null;
	
	String nid = "";
	
	TextView nname = null;
	TextView grade = null;
	TextView company = null;
	TextView department = null;
	TextView address = null;
	
	TextView email= null;
	TextView telNum = null;
	TextView phoneNum = null;
	
	int style_card = 0;
	
	ImageButton titleset = null;
	ImageButton modify = null;
	ImageButton delete = null;
	ImageButton bt_back = null;
	
	LinearLayout topTitle = null;
	
	
	int pos = -999;
	boolean flag = true;
	
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.view = inflater.inflate(R.layout.myview_layout, container, false);

		nname = (TextView)view.findViewById(R.id.myview_layout_tv_nname);
		grade = (TextView)view.findViewById(R.id.myview_layout_tv_grade);
		company = (TextView)view.findViewById(R.id.myview_layout_tv_company);
		department= (TextView)view.findViewById(R.id.myview_layout_tv_department);
		address = (TextView)view.findViewById(R.id.myview_layout_tv_address);
		
		email= (TextView)view.findViewById(R.id.myview_layout_tv_email);
		telNum = (TextView)view.findViewById(R.id.myview_layout_tv_telNum);
		phoneNum = (TextView)view.findViewById(R.id.myview_layout_tv_phoneNum);
		
		titleset = (ImageButton)view.findViewById(R.id.myview_titleset);
		modify= (ImageButton)view.findViewById(R.id.myview_modify);
		delete = (ImageButton)view.findViewById(R.id.myview_delete);
		bt_back = (ImageButton)view.findViewById(R.id.myviewLayout_imgBtnBack);
		
		
		
		this.topTitle = (LinearLayout)this.view.findViewById(R.id.myviewlayout_top);
		this.topTitle.setOnClickListener(this);
		
		
		
		
		
		
		MainActivity.page_check = new MainActivity().BACK_MYLIST;
		
		bundle = getArguments();
		
		
		viewview(bundle);
		
		titleset.setOnClickListener(this);
		modify.setOnClickListener(this);
		delete.setOnClickListener(this);
		bt_back.setOnClickListener(this);
		
		return view;
	}
	
	
	@Override
	public void onResume() {
		new NfcOnOffChange(this.topTitle, R.drawable.top_bar_on_nfc_my, R.drawable.top_bar_off_nfc_my);
		
		
		
		
		super.onResume();
	}
	
	
	
	public void viewview(Bundle bundle)
	{
		DBHelper db = new DBHelper(getActivity(), "toyou", null, 1);
		data = new Data();
		nid = getArguments().getInt("id")+"";
		data = db.selectone(nid);
		
		nname.setText(data.getNname());
		grade.setText(data.getGrade());
		company.setText(data.getCompany());
		department.setText(data.getDepartment());
		address.setText(data.getAddress());
		
		email.setText(data.getEmail());
		telNum.setText(data.getTelNum());
		phoneNum.setText(data.getPhoneNum());
		
		
		
		style_card = data.getStyleId();
		
		Button menu = (Button)this.view.findViewById(R.id.myViewLayout_handle);
		menu.setText("대표명함");
		
		if(this.data.getMtitle() == 1){
			menu.setTextColor(Color.parseColor("#27CFCF"));
		}else{
			menu.setTextColor(Color.parseColor("#888888"));
		}
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		this.trans = getFragmentManager().beginTransaction();
		final DBHelper db = new DBHelper(getActivity(), "toyou", null, 1);
		
		switch (id) {
		case R.id.myview_titleset:
			
			
			data = db.selectone(nid);
			
			final Data d = data;
					
			boolean check = false;
			if(d.getMtitle()==1)
			{
				Toast.makeText(getActivity(), "이미 대표명함 입니다.", Toast.LENGTH_SHORT).show();
			}
			else
			{
				check = db.modifyMineTitle(d.getNid());
							
				if(check)
				{
					Toast.makeText(getActivity(), "대표명함 지정 성공", Toast.LENGTH_SHORT).show();
					MainActivity.my_main_namecard = data;
					
					frag = new MyFragment();
					trans.replace(R.id.root_layout, frag);
					trans.addToBackStack(null);
					trans.commitAllowingStateLoss();
				}
				else
				{
					Toast.makeText(getActivity(), "대표명함 지정 실패", Toast.LENGTH_SHORT).show();
				}
			}
			break;
			
		case R.id.myview_modify :
			this.bundle = new Bundle();
			this.bundle.putString("id", this.nid);
			
			this.frag = new ModifyFragment();
			this.frag.setArguments(this.bundle);
			this.trans.replace(R.id.root_layout, this.frag);
			this.trans.addToBackStack(null);
			this.trans.commitAllowingStateLoss();

			break;
		
		case R.id.myview_delete :

			AlertDialog.Builder ad = new AlertDialog.Builder(getActivity());
			
			ad.setTitle("명함 삭제");
			ad.setMessage("명함을 삭제하시겠습니까?");
			
			ad.setNegativeButton("확인", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if(data.getMtitle()==1)
					{
						Toast.makeText(getActivity(), "대표명함은 삭제하실수 없습니다.", Toast.LENGTH_SHORT).show();
					}
					else
					{
						boolean b = db.delete(data.getNid());
						if(b){
							frag = new MyFragment();
							
							trans.replace(R.id.root_layout, frag);
							trans.addToBackStack(null);
							trans.commitAllowingStateLoss();
							Toast.makeText(getActivity(), "정상적으로 삭제되었습니다.", Toast.LENGTH_SHORT).show();
						}else{
							Toast.makeText(getActivity(), "삭제 중 오류가 발생했습니다.", Toast.LENGTH_SHORT).show();	
						}
					}
					
				}
			});
			
			ad.setPositiveButton("취소", null);
			
			
			ad.show();
			
			break;
		case R.id.myviewLayout_imgBtnBack:
			trans = getFragmentManager().beginTransaction();
			frag = new MyFragment();
			frag.setArguments(this.bundle);
			trans.replace(R.id.root_layout, frag);
			trans.addToBackStack(null);
			trans.commitAllowingStateLoss();
			break;

		case R.id.myviewlayout_top:
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
	            Intent intent = new Intent(Settings.ACTION_NFC_SETTINGS);
	            startActivity(intent);
	         } else {
	            Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
	            startActivity(intent);
	         }
			break;
		}
		
		
	}
}
